package com.bookyourtrek.action;

import java.math.BigInteger;
import java.util.Map;

import com.bookyourtrek.model.OrderModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CancelTicketAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private BigInteger cancelorderid;
	

	public BigInteger getCancelorderid() {
		return cancelorderid;
	}

	public void setCancelorderid(BigInteger cancelorderid) {
		this.cancelorderid = cancelorderid;
	}

	public String cancelticket() {
		return SUCCESS;
	}
	
	@SuppressWarnings("rawtypes")
	public String confirmcancelticket() {
		
		if(cancelorderid == null) {
			addFieldError("emptyorderid", "Please give orderID");
			return INPUT;
		}
		
		OrderModel om = new OrderModel();
		Map sess=ActionContext.getContext().getSession();
		String useremail = (String) sess.get("useremail");
		//check for orderid found with user email
		if(!om.orderIdExistWithUser(cancelorderid, useremail)) {
			addFieldError("orderidnotfound", "No order associated with this orderID");
			return INPUT;
		}
		//check for date validity
		if(om.ticketEndDatePassed(cancelorderid)) {
			addFieldError("eventover", "Event associated with this orderID is already over. It cannot be cancelled.");
			return INPUT;
		}
		//check if not already cancelled
		if(om.ticketAlreadyCancelled(cancelorderid, useremail)){
			addFieldError("alreadycancelled", "This order is already cancelled. Check your order summary");
			return INPUT;
		}
		
		//cancel order and give message about successful cancellation
		om.cancelorder(cancelorderid, useremail);
		//send mail about cancellation
		return SUCCESS;
	}

}
